package com.sy.controller;

import com.sy.dto.JsonResult;
import com.sy.entity.Homework;
import com.sy.sevice.AdminService;
import com.sy.vo.GetNoticeByConditions;
import com.sy.vo.HomeworkVo;
import com.sy.vo.ModifyAdminVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author csd
 * @version 1.0
 * @date 2020/9/14
 * @usage 管理员类相关的控制器
 */
@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 获取最新的5条公告在首页进行显示
     *
     * @param
     * @return JsonResult对象（notice实体类）
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = {"sortBy", "order", "pageSize"})
    public JsonResult getNoticesOrderByCtime(@RequestParam("sortBy") String sortBy, @RequestParam("order") String order, @RequestParam("pageSize") int pageSize) {
        System.out.println(sortBy);
        System.out.println(order);
        System.out.println(pageSize);
        return adminService.getNoticesByConditions(new GetNoticeByConditions(sortBy, order, pageSize, null));
    }

    /**
     * 通过公告id获取具体的公告信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public JsonResult getNoticeByNoticeId(@PathVariable("id") Integer id) {
        System.out.println("id:" + id);
        return adminService.getNoticeByNoticeId(id);
    }

    /**
     * 查看公告 并进行每页10条分页 分页由前段完成 此处只要获取所有公告并进行排序即可
     *
     * @param sortBy
     * @param order
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = {"sortBy", "order"})
    public JsonResult getNoticesOrderByCtime(@RequestParam("sortBy") String sortBy, @RequestParam("order") String order) {
        return adminService.getNoticesByConditions(new GetNoticeByConditions(sortBy, order));
    }


    /**
     * 通过关键词搜索公告内容
     *
     * @param keyWord
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET, params = {"keyWord"})
    public JsonResult getNoticeByKeyWord(@RequestParam("keyWord") String keyWord) {
        System.out.println("keyWord:" + keyWord);
        return adminService.getNoticesByConditions(new GetNoticeByConditions(keyWord));
    }

    /**
     * 新增作业到数据库
     *
     * @param homeworkVo
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public JsonResult addHomework(@RequestBody HomeworkVo homeworkVo) {
        System.out.println(homeworkVo);
        Homework homework = new Homework(homeworkVo.getTitle(), homeworkVo.getCtime(), homeworkVo.getEtime(), homeworkVo.getTeacherid(), homeworkVo.getCourseid());
        return adminService.addHomework(homework);
    }

    //上传作业到对应文件夹


    /**
     * @param modifyAdminVo 管理员信息携带对象
     * @return 修改成功的管理员信息
     * @author JianBo-Sheng
     * @version 1.0
     * @date 2020/9/14 16:31
     * 修改管理员个人信息
     */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public JsonResult updateAdmin(@RequestBody ModifyAdminVo modifyAdminVo) {
        System.out.println(modifyAdminVo);
        return adminService.modifyAdmin(modifyAdminVo);
    }
}
